package com.wzm.org.controller;

import com.wzm.basic.annotation.PreAuthorize;
import com.wzm.basic.util.AjaxResult;
import com.wzm.basic.util.PageList;
import com.wzm.org.domain.Department;
import com.wzm.org.query.DepartmentQuery;
import com.wzm.org.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
@Api(value = "部门的api",description = "部门相关的crud功能")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * 接口:查询所有部门
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有部门")
    public List<Department> findAll(){
        return departmentService.findAll();
    }

    /**
     * 接口：查询单个部门
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个部门")
    Department findOne(@PathVariable("id") Long id){
        return departmentService.findOne(id);
    }

    /**
     * 接口：分页查询
     * @param query
     * @return
     */
    @PostMapping
    @PreAuthorize(name = "部门列表",sn = "department:list") //value后面用来做权限校验
    @ApiOperation(value = "分页查询")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query){
        return departmentService.queryPage(query);
    }

    /**
     * 接口：通过id删除部门
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "部门删除",sn = "department:delete") //value后面用来做权限校验
    @ApiOperation(value = "通过id删除部门")
    public AjaxResult del(@PathVariable("id") Long id ){
        try {
            departmentService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 接口：新增修改
     * @param department
     * @return
     */
    @PutMapping
    @ApiOperation(value = "新增或者修改部门")
    @PreAuthorize(name = "部门新增和修改",sn = "department:save") //value后面用来做权限校验
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if(department.getId()==null){//没有id就是 新增 否则就是修改
                departmentService.add(department);
            }else {
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();

            return new AjaxResult(false,"操作失败");
        }
    }
    @PatchMapping
    @ApiOperation("批量删除接口")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            departmentService.patchDel(ids);
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败了 ");
        }
    }
    @GetMapping("/deptTree")
    @ApiOperation("查询所有部门树")
    public List<Department> deptTree(){
       return departmentService.deptTree();
    }
}
